const db = wx.cloud.database();
const command = db.command;
Component({
  options : {
    multipleSolts :true //定义组件支持多solts
  },
  /*
   * 组件的属性列表 
   */
  properties : {
    //提示语
     hint : {
      type : String,
      value : "添加图片"
    } ,
    // 添加图片按钮的icon
    hint_icon : {
      type : String,
      value : "iconfont icon-xiangji"
    },
    // 添加图片的按钮的icon的大小
    icon_size : {
      type : String,
      value: "100rpx"
    },
    // 添加图片的按钮的颜色
    icon_color : {
      type : String,
      value : "#2C2C2C"
    },
    //提示块的背景颜色
    hint_background : {
      type : String ,
      value: "#E5E5E5"
    },
    //每块的高 高宽比默认1:1
    hint_height : {
      type : String,
      value : "160rpx"
    },
    //每块的宽 高宽比默认1:1
    hint_width : {
      type : String,
      value : "160rpx"
    },
    //整体背景颜色
    background : {
      type : String,
      value: "#FFFFFF"
    },
    //图片剪裁mode
    mode : {
      type : String,
      value: "aspectFill"
    },

    // 从页面传递过来的报修时间
    startDate : {
      type : String,
    },
    // 从页面传递过来的报修地点
    textarea : {
      type : String,
    },
    // 从页面传递过来的临时图片地址
    picsrc : {
      type : Array,
    },
    describe_fix_desc:{
      type : String,
    },
    useropenid:{
      type:String
    }




  },
  //内部数据
  data : {
    image: [],

  },
  //组件方法列表
  methods : {

    // 上传图片
    chooseImage:  function () {
      var that = this;
      wx.chooseImage({
        count: 9,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success: function (res) {
          var tempFilePaths = res.tempFilePaths;//用户选择的图片的本地路径列表
          var length = tempFilePaths.length//用户选了几张
          //将路径循环push入image数组中
          var i = 0
          while (length) {
            var obj = {}
            obj.imageHide = ""
            obj.imageAddress = tempFilePaths[i]
            that.data.image.push(obj)
            length--
            i++
          }
          //使body自适应图片的增加
          var l = that.data.image.length
          if (l >= 3) {
            var b = parseInt(l / 3)
            that.setData({
              bodyStyle: "height:" + (328 + b * 90) + "px"
            })
          }
          //重新渲染image视图
          that.setData({
            image: that.data.image
          })
        },
        fail: function () {

        },
      })
      this.triggerEvent('getpicsrc',this.data.image)
      // console.log(this.data.image);
    },
    // 删除图片
    removeImage: function (event) {

      //删除相应的数组元素
      this.data.image.splice(event.currentTarget.id, 1)
      //重新渲染视图
      this.setData({
        image: this.data.image
      })
      this.triggerEvent('getpicsrc',this.data.image)
      // console.log(this.data.image);
    },

    // 按钮上传所有数据
    upload: async function(){
      var nowtime =  new Date().toLocaleString();
      var useropenid = this.data.useropenid;
      var date = this.data.startDate;
      var area = this.data.textarea;
      var pic = this.data.image;
      var desc = this.data.describe_fix_desc;
      console.log(nowtime);
      console.log(date);
      console.log(area);
      // console.log(pic[0].imageAddress);
      console.log(desc);
      if(pic.length == 0){
        // console.log("用户没有上传任何文件");
        wx.showModal({
          title:"提示",
          showCancel:false,
          content:"请至少上传一张故障图片",
        })
        return false
      }else if(area.length == 0){
        wx.showModal({
          title:"提示",
          showCancel:false,
          content:"请选择维修地点",
        })
      }else if(date=="请选择预约时间，方便上门服务"){
        wx.showModal({
          title:"提示",
          showCancel:false,
          content:"请选择预约时间",
        })
      }else if(desc.length == 0){
        wx.showModal({
          title:"提示",
          showCancel:false,
          content:"请选择预约时间",
        })
      }else{
        wx.showModal({
          title: '提示',
          content: '是否确认提交',
          success (res) {
            var uploaded_img = [];
            if (res.confirm) {
              console.log('用户点击确定')
              wx.showLoading({
              title: '提交中',
              mask:true,
              })
              function useCloudStorage(num) {
                console.log(pic[num].imageAddress);
                const tempFilePaths = pic[num].imageAddress;
                return wx.cloud.uploadFile({
                  cloudPath:'fix_pic/'+(new Date()).valueOf()+'_'+num+'.png', // 文件名
                  filePath:tempFilePaths
                }).then(res=>{
                  uploaded_img.push(res.fileID)
                  console.log(uploaded_img);
                })
              }
              for(let num = 0;num<pic.length;num++){
              }
              const promiseArr = [];
              for (let num = 0;num<pic.length;num++) {
                promiseArr.push(useCloudStorage(num));
              }
              Promise.all(promiseArr).then(() => {
                useDB();
              });
              function useDB() {
                // 隐藏加载中圈圈
                wx.hideLoading()
                // 显示提交成功
                wx.showToast({
                title: '提交成功',
                icon:"success",
                mask:true,
                duration:2000,
                });
                // 拿到所有数据以后上传到数据库
                return db.collection("fix_desc").add({
                  data:{
                    openid:useropenid,
                    fix_appointment_time:date,
                    fix_submit_pos:area,
                    fix_submit_desc:desc,
                    fix_submit_time:nowtime,
                    fix_pic:uploaded_img,
                    fix_state:"已提交",
                    worker:'',
                    receiveorder_time:'',
                    fix_finishtime:'',

                  },
                  success:function(res){
                    console.log("添加数据库成功",res);
                    wx.switchTab({
                      url: '../../pages/main/main',
                    })
                    console.log("开始传送");
                    wx.reLaunch({
                      url: '../../pages/me/me',
                    })
                  },
                  fail:function(res){
                    console.log("添加数据库失败",res);
                  }
                })
              }
            } 
            else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
     
    },
  },
})